;;(function($){

    //变量区
    var handleData = $.Callbacks();
    var curID = null;

    //数据仓库
    var GLOBAL_STORE = {
        carts : getCarts(),
    }
    console.log(GLOBAL_STORE)

    // 初始化获取购物车；
    function getCarts(){
        if(localStorage.getItem("carts")){
                return JSON.parse(localStorage.getItem("carts"))
        }else{
                return [];
        }
    }

    // 保存数据;
    function saveData(type,json){
        localStorage.setItem(type,JSON.stringify(json));
    }

/**
 *  @function renderPage 渲染页面;
 *  @function init 初始化页面;
 * 
 */

    //基础页面渲染
    function renderPage(data){
        var pricedata = data.performBases[0].performs[0];
        var price = pricedata.skuList;
        let [html,htmltitle,htmltime,htmlshowtime,htmlprice] =
            ['','','','','']
        //图片
        html += `<span class="tip">总票代</span>
                <img alt="" class="poster"
                    data-src="//img.alicdn.com/imgextra/i1/2251059038/O1CN01XkbKqF2GdSAftFBWO_!!0-item_pic.jpg_q60.jpg_.webp"
                    src="${pricedata.verticalPic}"
                    lazy="loaded" />`

        $(".cover").html(html);

        //标题
        htmltitle += `【${pricedata.venueCity}】${pricedata.name}`
        $('.title').html(htmltitle);

        htmltime +=`<div class="time">
                        时间：${pricedata.showTime}
                    </div>
                    <div class="place">
                            <div class="addr">
                                场馆：${pricedata.venueCity} | ${pricedata.venueName}
                            </div>
                            <a data-spm="daddress" href="//ditu.amap.com/regeo?lng=106.505837&amp;lat=29.378358&amp;name=华熙文化体育中心&amp;src=damai&amp;callnative=0&amp;platform=pc" target="_blank" class="mapicon"></a>
                    </div>`
        $(".address").html(htmltime);

        //演出时间
        htmlshowtime +=`<div class="select_right_list_item active">
                                <span>${pricedata.showTime}</span>
                        </div>`
        $(".showtime").html(htmlshowtime);    
        
        //票档价格
        $.each(price,(key,item) => {
            // GLOBAL_STORE.pxxList[key] = item.price;
            htmlprice +=`<div class="select_right_list_item sku_item" data-id="${item.skuId}">
                                <div class="skuname" data-id="${item.price}">
                                    ${item.skuName}
                                </div>
                        </div>`
        })
        $(".showprice").html(htmlprice); 

    }

    //选择不同的票档
    function gaindata(){
        
        $('.sku_item').removeClass("active");
        $(this).addClass('active');

        var id = $(this).attr("data-id");
        curID = id;

        if(!(GLOBAL_STORE.carts instanceof Array)){
            GLOBAL_STORE.carts = [];
        }

        var hasSameGoods = GLOBAL_STORE.carts.some( item => {
            if(item.id == id){
                // item.count ++;
                return true;
            }
        })

        if(!hasSameGoods){
            GLOBAL_STORE.carts.push({
                  id : id,
                  count : 1,
                  price : $(this).children().attr("data-id"),
            });
        }
        renderPrice();
    }

    //获取数据后渲染数量和价格
    function renderPrice(){
        var html = '';
        var htmlnumber = '';

        GLOBAL_STORE.carts.forEach( item => {
            if (item.id == curID){

                htmlnumber = `<input data-v-1bfbfb02="" autocomplete="off"
                        spellcheck="false" placeholder="${item.count}"
                        class="cafe-c-input-number-input"
                        data-spm-anchor-id="a2oeg.project.projectinfo.i3.c912308ctkLGLs" />`

                html += `<i>￥</i><span>${(item.price * item.count).toFixed(2)}</span>`
                $('.number_wrap').html(htmlnumber);
                $('.totol__price').html(html);

            }
            
                
        })
        
    }
    renderPrice();


    //增加数量
    function add(){
        var id = $('.showprice .active').attr("data-id");
        // console.log(id)

        GLOBAL_STORE.carts.some( item => {
            if(item.id == id){
                item.count ++;
                item.count= item.count > 2 ? 2 : item.count;

            }
        })

        renderPrice();
        saveData("carts",GLOBAL_STORE.carts)
    }


    //减少数量
    function reduce(){
        
        var id = $('.showprice .active').attr("data-id");
        // console.log(id)

        GLOBAL_STORE.carts.some( item => {
            if(item.id == id){
                item.count --;
                console.log(typeof(item.count));
                item.count= item.count < 0 ? 0 : item.count;
            }
        })
        renderPrice();
        saveData("carts",GLOBAL_STORE.carts)
    }
    

    //初始化程序
    function init(){
        loaddata();
        bindEvent();
    }


    //绑定事件
    function bindEvent(){
        handleData.add(renderPage);

        $(".showprice").on("click",".sku_item",gaindata)
        $(".add").on("click",add);
        $(".reduce").on("click",reduce);
    }


    //数据请求
    function loaddata(){
        fetch("./json/shopping.json")
        .then(res => {
            return res.json()
        })
        .then(handleData.fire)
    }


    //功能调用初始化
    $(init);
        
})(jQuery);